package com.szh.cloud.login.control;

import com.szh.cloud.login.entity.LoginUserEntity;
import com.szh.cloud.login.entity.MenuLevelEntity;
import com.szh.cloud.login.service.LoginService;
import com.szh.cloud.mybatis.entity.PtUserEntity;
import com.szh.cloud.spring.control.BaseController;
import com.szh.cloud.user.entity.UserEntity;
import com.szh.cloud.user.execption.RepeatEmailException;
import com.szh.cloud.user.execption.RepeatMobileException;
import com.szh.cloud.user.execption.RepeatUserNameException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MainController extends BaseController {

    @Resource
    private HttpSession session;
    @Resource
    private LoginService loginService;

    private static Logger logger = LogManager.getLogger(MainController.class);
    @RequestMapping("/")
    public String mainPage(){
        LoginUserEntity entity = (LoginUserEntity)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        logger.info("userName:" + entity.getUsername());
        session.setAttribute("lang", LocaleContextHolder.getLocale().toString());
        List<MenuLevelEntity> list = loginService.queryMenuList();
        session.setAttribute("menuList",list);
        return "login/mainPage";
    }

    @RequestMapping("/welcome")
    public String welcomePage(){
        return "login/welcome";
    }

    @RequestMapping("/temp")
    public String tempPage(){

        return "login/temp";
    }

    @RequestMapping("/queryUserList")
    @ResponseBody
    public PtUserEntity queryUserList(){
        LoginUserEntity entity = (LoginUserEntity)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        PtUserEntity  entity1 = loginService.queryUserListById(entity.getUserUuid());
        return  entity1;
    }


    @RequestMapping("/modifyUserInfo")
    @ResponseBody
    public String modifyUserInfo(UserEntity userEntity){
        LoginUserEntity entity = (LoginUserEntity)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        userEntity.setUserUuid(entity.getUserUuid());
        try {
            loginService.modifyUserInfo(userEntity);
        } catch (RepeatUserNameException e) {
            return failJSON("user.user.name.repeat");
        } catch (RepeatEmailException e) {
//            e.printStackTrace();
            return failJSON("user.email.repeat");
        } catch (RepeatMobileException e) {
//            e.printStackTrace();
            return failJSON("user.mobile.repeat");
        }
        session.setAttribute("niceName",userEntity.getNiceName());
        return successJSON("user.modify.success");
    }


/*    @RequestMapping("/queryMenuList")
    @ResponseBody
    public String queryMenuList(){
        List<MenuLevelEntity> list=loginService.queryMenuList();
        return JSON.toJSONString(list);
    }*/
}
